package com.acme.bankapp.domain.bank;

import com.acme.bankapp.domain.bank.exceptions.ClientExistsException;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by anton on 8/18/14.
 */
public class Bank implements Serializable {
    private final int maxClients = 10;
    private int activeClients = 0;
    private Client[] clients = new Client[maxClients];
    private ClientRegistrationListener[] listeners;
    private DebugListener debugListener = null;


    // Constructor
    public Bank(Client[] clients) {
        this.clients = clients;
    }
    public Bank() {
        this.debugListener = new DebugListener();
        this.listeners = new ClientRegistrationListener[]{new EmailNotificationListener(), new PrintClientListener(), };
    }

    public Client[] getClients() {
        Client[] cls = new Client[activeClients];
        System.arraycopy(clients, 0, cls, 0, activeClients);
        return cls; //clients;
    }

    public int getMaxClients() {
        return maxClients;
    }

    public Client getClientByName(String name) {
        for (Client client : this.getClients()) {
            if (client != null && client.getName().equals(name)) {
                return client;
            }
        }
        return null;
    }

    public void addClient(Client client) throws ClientExistsException {
        if(this.activeClients >= this.maxClients) {
            System.out.println("Max limit clients exceeded");
        }
        if (getClientByName(client.getName()) != null) {
            throw new ClientExistsException(client.getName());
        }

        clients[activeClients++] = client;

        // Calling listener
        for(ClientRegistrationListener l : listeners) {
            l.onClientAdded(client);
        }
        debugListener.printInfo(client);
    }

    private static class DebugListener implements Serializable {
        void printInfo(Client client) {
            System.out.println(client.getName() + " " + new Date());
        }
    }

    private static class EmailNotificationListener implements ClientRegistrationListener, Serializable {
        @Override
        public void onClientAdded(Client client) {
            System.out.println("=========================");
            System.out.println("Notification email for client " + client.getName() + " to be sent");
        }
    }

    private static class PrintClientListener implements ClientRegistrationListener, Serializable {
        @Override
        public void onClientAdded(Client client) {
            System.out.println("=================================");
            System.out.println("Information about the client:");
            System.out.println("Name: " + client.getName());
            System.out.println("Gender: " + client.getGender());
            System.out.println("Balance: " + client.getAccount().getBalance());
        }
    }

    public String getClientsInfo() {
        StringBuffer stringBuffer = new StringBuffer();
        for (Client c: this.getClients()) {
            stringBuffer.append(c.toString());
        }
        System.out.println(stringBuffer);
        return stringBuffer.toString();
    }
}
